package com.sysu.homework.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.hibernate.validator.xml.GroupSequenceType;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionSupport;
import com.sysu.homework.model.Group;
import com.sysu.homework.model.HomeworkResult;
import com.sysu.homework.model.Student;
import com.sysu.homework.service.GroupService;
import com.sysu.homework.service.HomeworkResultService;
import com.sysu.homework.service.StudentService;

@Component("indexAction")
@Scope("prototype")
public class IndexAction extends ActionSupport{
	private HomeworkResultService homeworkResultService;
	private List<HomeworkResult> results = new ArrayList<HomeworkResult>();
	private Group group;
	private GroupService groupService;
	private StudentService studentService;
	private Student student;
	private List<Group> groups;

	private HttpSession session = null;
	private String role;
	private int userid;
	
	
	public IndexAction() {
		session = ServletActionContext.getRequest().getSession();
		setRole(session.getAttribute("role").toString());
		setUserid((Integer)session.getAttribute("user"));
	}

	@Override
	public String execute() throws Exception {
		role = getRole();
		userid = getUserid();
		if(role.equals("student")){
			results = homeworkResultService.getMyHomeworkResults(userid);
			student = studentService.getStudentById(userid);
			int groupid = studentService.getGroupId(userid);
			group = groupService.getGroupById(groupid);
			
		}else if (role.equals("ta") || role.equals("teacher"))
			groups = groupService.getAllGroups();
		return "success";
	}

	@Resource
	public void setHomeworkResultService(HomeworkResultService homeworkResultService) {
		this.homeworkResultService = homeworkResultService;
	}

	public HomeworkResultService getHomeworkResultService() {
		return homeworkResultService;
	}
	public List<HomeworkResult> getResults() {
		return results;
	}

	public void setResults(List<HomeworkResult> results) {
		this.results = results;
	}

	public HttpSession getSession() {
		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}

	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}

	public int getUserid() {
		return userid;
	}

	public void setUserid(int userid) {
		this.userid = userid;
	}

	public void setGroup(Group group) {
		this.group = group;
	}

	public Group getGroup() {
		return group;
	}

	public GroupService getGroupService() {
		return groupService;
	}

	@Resource
	public void setGroupService(GroupService groupService) {
		this.groupService = groupService;
	}

	@Resource
	public void setStudentService(StudentService studentService) {
		this.studentService = studentService;
	}

	public StudentService getStudentService() {
		return studentService;
	}

	public void setStudent(Student student) {
		this.student = student;
	}

	public Student getStudent() {
		return student;
	}

	public void setGroups(List<Group> groups) {
		this.groups = groups;
	}

	public List<Group> getGroups() {
		return groups;
	}

	

}
